package com.ruoyi.web.controller.data;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.data.domain.MyProcess;
import com.ruoyi.data.domain.MyRelationship;
import com.ruoyi.data.service.MyProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.net.URLDecoder;
import java.util.Map;

/**
 * 【关系图】Controller
 *
 * @author ruoyi
 * @date 2023-01-10
 */
@Controller
@RequestMapping("/data/process")
public class MyProcessController extends BaseController
{
    private String prefix = "data/process";

    @Autowired
    private MyProcessService myProcessService;

    @GetMapping()
    public String process()
    {
        return prefix + "/process";
    }

    /**
     * 关系图数据
     */
    @GetMapping("/list")
    @ResponseBody
    public AjaxResult list()
    {
        Map<String, Object> stringObjectMap = myProcessService.selectMyProcessList();

        return AjaxResult.success(stringObjectMap);

    }


    /**
     * 新增【新增节点】
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存【新增节点】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MyProcess myProcess)
    {
        return toAjax(myProcessService.insertMyProcess(myProcess));
    }

    /**
     * 新增【新增关系】
     */
    @GetMapping("/addMyRelationship")
    public String addMyRelationship()
    {
        return prefix + "/addMyRelationship";
    }

    /**
     * 新增保存【保存关系】
     */
    @Log(title = "【保存关系】", businessType = BusinessType.INSERT)
    @PostMapping("/addSaveMyRelationship")
    @ResponseBody
    public AjaxResult addSaveMyRelationship(MyRelationship myRelationship)
    {
        return toAjax(myProcessService.insertMyRelationship(myRelationship));
    }

    /**
     * 修改【请填写功能名称】
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        MyProcess myProcess = myProcessService.selectMyProcessById(URLDecoder.decode(id));
        mmap.put("myProcess", myProcess);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MyProcess myProcess)
    {
        return toAjax(myProcessService.updateMyProcess(myProcess));
    }

    /**
     * 删除【删除节点或者关系】
     */
    @Log(title = "【删除节点或者关系】", businessType = BusinessType.DELETE)
    @PostMapping( "/deleteNodeOrRelationship")
    @ResponseBody
    public AjaxResult remove(String name)
    {
        String res = myProcessService.deleteNodeOrRelationship(name);

        return AjaxResult.success(res);
    }
}